package com.molyfun.parents.activity.msg.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.listactivity.viewholder.MsgViewHolder;
import com.molyfun.parents.activity.setting.view.MsgItemView;
import com.molyfun.parents.rest.bean.MsgListResponse;

import java.util.HashSet;
import java.util.List;

import mabeijianxi.camera.util.Log;

/**
 *
 */
public class MsgListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements MsgItemView.MsgItemViewCallback, MsgItemView.MsgItemViewHomeWork {
    Context mContext;
    private List<MsgListResponse.MsgBean> mList;

    public MsgListAdapter(Context context) {
        this.mContext = context;
    }

    public MsgListAdapter(Context context, int category) {
        this.mContext = context;
    }

    private String TAG = "MsgListAdapter";


    public void refreshData(List<MsgListResponse.MsgBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void clearAll() {
        if (mList != null) {
            mList.clear();
            notifyDataSetChanged();
        }
    }

    public void addAll(List<MsgListResponse.MsgBean> list) {
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MsgViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_msg_cell, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MsgViewHolder msgViewHolder = (MsgViewHolder) holder;
        msgViewHolder.mMsgItemView.initData(mList.get(position), MsgItemView.MesgDataMode.MODE_BOTH, mCurrentMode, position == (getItemCount() - 1));
        Log.i(TAG ,"mMsgItemView.initData");
        msgViewHolder.mMsgItemView.setMsgItemViewCallback(this);
        msgViewHolder.mMsgItemView.setMsgItemViewCallbackMsgId(this);
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }
        return mList.size();
    }

    public HashSet<MsgListResponse.MsgBean> getmChoosedList() {
        return mChoosedList;
    }

    public void setmChoosedList(HashSet<MsgListResponse.MsgBean> mChoosedList) {
        this.mChoosedList = mChoosedList;
    }

    private HashSet<MsgListResponse.MsgBean> mChoosedList = new HashSet<>();


    @Override
    public void onMsgCheckChanged(MsgListResponse.MsgBean msgBean, boolean isChecked) {
        if (isChecked) {
            mChoosedList.add(msgBean);
        } else {
            mChoosedList.remove(msgBean);
        }
    }

    public void checkAllMsg() {
        if (mList != null) {
            for (MsgListResponse.MsgBean msgBean : mList) {
                if (!TextUtils.isEmpty(msgBean.getId())) {
                    msgBean.setIsChecked(true);
                    mChoosedList.add(msgBean);
                }
            }
            notifyDataSetChanged();
        }

    }

    public void uncheckAllMsg() {
        if (mList != null) {
            for (MsgListResponse.MsgBean msgBean : mList) {
                if (!TextUtils.isEmpty(msgBean.getId())) {
                    msgBean.setIsChecked(false);
                    mChoosedList.remove(msgBean);
                }
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 删除item
     */
    public void deleteCheckMsg() {
        if (mList != null) {
            mList.removeAll(mChoosedList);
            mChoosedList.clear();
            endChooseEditMode();
            notifyDataSetChanged();
        }
    }

    public void endChooseEditMode() {
        mCurrentMode = MsgListAdapter.MsgMode.MODE_NORMAL;
        mChoosedList.clear();
        notifyDataSetChanged();
    }

    public void beginChooseEditMode() {
        mCurrentMode = MsgListAdapter.MsgMode.MODE_CHOOSE;
        notifyDataSetChanged();
    }

    private int mCurrentMode = MsgListAdapter.MsgMode.MODE_NORMAL;

    @Override
    public void OnMsgChangeState(String mMsgId) {
        if (msgListAdapterCallback != null) {
            msgListAdapterCallback.setMsgId(mMsgId);
        }
    }

    /**
     * normal mode: show the course
     * choose mode: used as checkbox to record download task
     */
    public interface MsgMode {
        int MODE_NORMAL = 1001;
        int MODE_CHOOSE = 1002;
    }

    private MsgListAdapterCallback msgListAdapterCallback;

    public void setMsgListAdapterCallback(MsgListAdapterCallback msgListAdapterCallback) {
        this.msgListAdapterCallback = msgListAdapterCallback;
    }

    public interface MsgListAdapterCallback {
        void setMsgId(String msgId);
    }
}
